package com.data.center.controller;

import com.data.center.dto.OrganizationTagDTO;
import com.data.center.dto.OrganizationTagRelationshipDTO;
import com.data.center.entity.OrganizationTag;
import com.data.center.entity.OrganizationTagRelationship;
import com.data.center.exception.ResultBody;
import com.data.center.mapper.OrganizationTagMapper;
import com.data.center.mapper.OrganizationTagRelationshipMapper;
import com.data.center.service.OrganizationTagRelationshipService;
import com.data.center.service.OrganizationTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Li_Jx
 * @description: 组织标签关系表
 * @date 2023/11/30 18:25
 */
@RestController
@RequestMapping("/organizationTagRelationship")
@Slf4j
public class OrganizationTagRelationshipController {
    @Autowired
    private OrganizationTagRelationshipService organizationTagRelationshipService;
    @Autowired
    private OrganizationTagRelationshipMapper organizationTagRelationshipMapper;
    /**
     * @description 一个通用的查询接口
     * @author Li_Jx
     * @Date 2023/11/30 19:41
     */
    @GetMapping("/select")
    public ResultBody select(OrganizationTagRelationshipDTO organizationTagRelationshipDTO){
        OrganizationTagRelationship organizationTagRelationshipFiler = new OrganizationTagRelationship();
        org.springframework.beans.BeanUtils.copyProperties(organizationTagRelationshipDTO,organizationTagRelationshipFiler);
        List<OrganizationTagRelationship> organizationTagList = organizationTagRelationshipMapper.select(organizationTagRelationshipFiler);
        return ResultBody.success(organizationTagList);
    }

    /**
     * @description 通过id查询对象
     * @author Li_Jx
     * @Date 2023/11/30 19:41
     */
    @GetMapping("/findById")
    public ResultBody findById(String id){
        if(id == null || id.isEmpty()){
            return ResultBody.error("标签id为空");
        }
        OrganizationTagRelationship organizationTagRelationship = organizationTagRelationshipMapper.findById(id);
        if(organizationTagRelationship == null){
            return ResultBody.error("未查询到匹配结果");
        }
        return ResultBody.success(organizationTagRelationship);
    }

    /**
     * @description 一个通用的通过id更新接口
     * @author Li_Jx
     * @Date 2023/11/9 19:41
     */
    @PutMapping("/update")
    public ResultBody update(OrganizationTagRelationshipDTO organizationTagRelationshipDTO){
        OrganizationTagRelationship organizationTagRelationship = new OrganizationTagRelationship();
        org.springframework.beans.BeanUtils.copyProperties(organizationTagRelationshipDTO,organizationTagRelationship);
        try {
            organizationTagRelationshipService.update(organizationTagRelationship);
        }catch (RuntimeException e){
            return ResultBody.error(e.getMessage());
        }
        return ResultBody.success();
    }
    /**
     * @description 新增标签关系
     * @author Li_Jx
     * @Date 2023/11/30 16:46
     */
    @PostMapping("/insert")
    public ResultBody insert(OrganizationTagRelationshipDTO organizationTagRelationshipDTO){
        OrganizationTagRelationship organizationTagRelationship = new OrganizationTagRelationship();
        org.springframework.beans.BeanUtils.copyProperties(organizationTagRelationshipDTO,organizationTagRelationship);
        try {
            organizationTagRelationshipService.insert(organizationTagRelationship);
        }catch (RuntimeException e){
            return ResultBody.error(e.getMessage());
        }
        return ResultBody.success();
    }

    /**
     * @description 通过id删除标签
     * @author Li_Jx
     * @Date 2023/11/21 18:49
     */
    @DeleteMapping("/deleteById")
    public ResultBody deleteById(String id){
        if(id == null || id.isEmpty()){
            return ResultBody.error("标签id为空");
        }
        OrganizationTagRelationship organizationTagRelationship = organizationTagRelationshipMapper.findById(id);
        if(organizationTagRelationship == null){
            return ResultBody.error("未查询到匹配结果");
        }
        organizationTagRelationshipMapper.deleteById(id);
        return ResultBody.success();
    }

}
